package dao.Impl;

import bean.Husband;
import bean.Wife;
import dao.WifeDao;
import util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class WifeDaoImpl extends DruidUtil implements WifeDao {
    @Override
    public Wife findByWid(int wid) {
        Wife wife = new Wife();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement("select * from wife w,husband h where w.hid = h.hid and w.wid = ?");
            preparedStatement.setInt(1,wid);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                //1.分别获取各自的信息
                wife.setWname(resultSet.getString("wname"));
                Husband husband = new Husband();
                husband.setHname(resultSet.getString("hname"));
                //2.建立两者之间的联系
                wife.setHusband(husband);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(connection,preparedStatement,resultSet);
        }
        return wife;
    }

    @Override
    public Husband findByhid(int hid) {
        Husband husband = new Husband();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement("select * from wife w,husband h where w.hid = h.hid and h.hid = ?");
            preparedStatement.setInt(1,hid);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                //1.分别获取各自的信息
                Wife wife = new Wife();
                wife.setWname(resultSet.getString("wname"));
                husband.setHname(resultSet.getString("hname"));
                //2.建立两者之间的联系
                husband.setWife(wife);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(connection,preparedStatement,resultSet);
        }
        return husband;
    }
}
